#if !MONODROID
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;


namespace Devsoft.Utilities
{
    class Xml
    {
        private static XmlDocument _XmlDocumentHelper = null;

        public static XmlNode GetXMLNode(Object obj,string Objectname)
        {

            System.Type thisType = obj.GetType();

            if (_XmlDocumentHelper == null)
            {
                _XmlDocumentHelper = new XmlDocument();
            }//End If

            XmlDocument xDoc = _XmlDocumentHelper;
            XmlNode xNode;
            if (Objectname == string.Empty)
            {
                xNode = xDoc.CreateElement(thisType.FullName);
            }//End If
            else
            {
                xNode = xDoc.CreateElement(Objectname);
            }
            

            FieldInfo[] thisFields = thisType.GetFields(BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Instance);
            
            foreach (FieldInfo f in thisFields)
            {
                XmlNode xNodeField = xDoc.CreateElement(f.Name);
                object subObj = f.GetValue(obj);
                if (subObj == null)
                {
                    continue;
                }//End If
                
                if (f.FieldType == typeof(System.Drawing.Color))
                {
                    System.Drawing.Color c = (System.Drawing.Color) subObj;
                    xNodeField.InnerText = c.ToArgb().ToString();
                } //End If
                else if (f.FieldType == typeof(System.Drawing.Font))
                {
                    System.Drawing.Font font = (System.Drawing.Font)subObj;
                    XmlAttribute xAttr = null;

                    xAttr = xDoc.CreateAttribute("familyName");
                    xAttr.Value = font.FontFamily.Name;
                    xNodeField.Attributes.Append(xAttr);

                    xAttr = xDoc.CreateAttribute("Size");
                    xAttr.Value = font.Size.ToString();
                    xNodeField.Attributes.Append(xAttr);

                    xAttr = xDoc.CreateAttribute("GdiCharSet");
                    xAttr.Value = font.GdiCharSet.ToString();
                    xNodeField.Attributes.Append(xAttr);

                    xAttr = xDoc.CreateAttribute("FontStyle");
                    xAttr.Value = font.Style.ToString();
                    xNodeField.Attributes.Append(xAttr);

                    xAttr = xDoc.CreateAttribute("GraphicsUnit");
                    xAttr.Value = font.Unit.ToString();
                    xNodeField.Attributes.Append(xAttr);
                    

                }//End If
                else if (f.FieldType == typeof(System.Windows.Forms.ProfessionalColorTable))
                {
                    System.Type subType = subObj.GetType();
                    PropertyInfo[] props =  subType.GetProperties();
                    
                    for (int i = 0; i < props.Length ; i++)
                    {
                        if (props[i].PropertyType == typeof(System.Drawing.Color))
                        {
                            XmlNode xSubNode = xDoc.CreateElement(props[i].Name);
                            xSubNode.InnerText = ((System.Drawing.Color)props[i].GetValue(subObj, null)).ToArgb().ToString();
                            xNodeField.AppendChild(xSubNode);
                        }//End If
                    	
                    }//End For
                    
                } //End If
                else
                {
                    xNodeField.InnerText = subObj.ToString();
                }//End Else .. If


                xNode.AppendChild(xNodeField);

            }//End Foreach

            return xNode;
        }

        static public void SaveObject(object obj, string sFileName)
        {
            XmlNode xNode = GetXMLNode(obj,string.Empty);
            XmlDocument xDoc = xNode.OwnerDocument;
            XmlDeclaration xmldecl;
            xmldecl = xDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xDoc.InsertBefore(xmldecl, xNode);
            xDoc.Save(sFileName);
            xDoc.RemoveAll();
        }

        static public string GetXMLString(Object obj)
        {
            return GetXMLNode(obj,string.Empty).OuterXml;
        }

    }
}
#endif